/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasyleagueFacade;
import beans.ejb.FantasyteamFacade;
import entities.Fantasyleague;
import entities.Fantasyteam;
import entities.Fantasyuser;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author The Lunatics
 */
@ManagedBean
@SessionScoped
public class JoinLeague {

    @EJB
    private FantasyleagueFacade fantasyleagueFacade;
    @EJB
    private FantasyteamFacade fantasyteamFacade;
    private AccountBean account;
    private List<Fantasyleague> leagues;
    private HtmlDataTable leagueDataTable;

    public HtmlDataTable getLeagueDataTable() {
        return leagueDataTable;
    }

    public void setLeagueDataTable(HtmlDataTable leagueDataTable) {
        this.leagueDataTable = leagueDataTable;
    }

    /**
     * Creates a new instance of JoinLeague
     */
    public JoinLeague() {
        fantasyleagueFacade = new FantasyleagueFacade();
        fantasyteamFacade = new FantasyteamFacade();
    }

    public List<Fantasyleague> getLeagues() {
        System.err.append("getLeagues");
        if (leagues == null) {
            leagues = fantasyleagueFacade.findAll();
        }
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        account = (AccountBean) request.getSession().getAttribute("accountBean");
        List<Fantasyleague> myLeagues = account.getMyLeagues();

//        for (Iterator<Fantasyleague> it = leagues.iterator(); it.hasNext();) {
//            Fantasyleague fantasyleague = it.next();
//            for (Iterator<Fantasyleague> it1 = myLeagues.iterator(); it1.hasNext();) {
//                if 
//
//            }
//        }
        return leagues;
    }

    public void setLeagues(List<Fantasyleague> leagues) {
        this.leagues = leagues;
    }

    public String join() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        account = (AccountBean) request.getSession().getAttribute("accountBean");

        Fantasyleague league = (Fantasyleague) leagueDataTable.getRowData();
        account.setCurrentLeague(league);
        account.setMyLeagues(null); // reset data to make the bean reload the DB

        Fantasyteam team = new Fantasyteam();
        Fantasyuser user = account.getAccount();
        team.setUserId(user);
        team.setLeagueId(league);
        team.setName(account.getLoginName());
        team.setCurrentLose(0);
        team.setCurrentWin(0);
        team.setCurrentWithdraw(0);
        fantasyteamFacade.create(team);

        return "standing";
    }
}
